import {z} from "zod";

export const UserSchema = z.object({
    userId: z.number().min(1,"id不能为空"),
    username: z.string().nonempty("用户名不能为空"),
    email: z.string().nonempty("邮箱不能为空").email("邮箱格式不正确"),
    sex: z.enum(['male','female',"all"]).default('all').transform((data)=>data.toString()),
    status: z.enum(['active','freeze','banned',"all"]).default('all').transform((data)=>data.toString()),
    password: z.string().nonempty("密码不能为空").min(6, "密码长度不能少于6个字符").max(30, "密码长度限制在三十个字符内"),
    createdAt: z.date({message:"创建时间不能为空"}),
});

export const LoginSchema = UserSchema.pick({password:true,email:true})

export const RegisterSchema = z.object({
    username: z.string().nonempty("用户名不能为空").max(20, "用户名长度限制在二十个字符内"),
    image: z.string().nonempty("头像不能为空"),
    email: z.string().nonempty("邮箱不能为空").max(200, "邮箱长度限制在两百个字符内").email("邮箱格式不正确"),
    sex: z.enum(['male','female']).default('male').transform((data)=>data.toString()),
    password: z.string().nonempty("密码不能为空").min(6, "密码长度不能少于6个字符").max(30, "密码长度限制在三十个字符内"),
    pwd: z.string().nonempty("密码不能为空").min(6, "密码长度不能少于6个字符").max(30, "密码长度限制在三十个字符内")
}).refine((data) => {
    return data.pwd === data.password;
}, {
    path: ["pwd"],
    message: "两次密码输入不一致"
});


export const UserListSerachFormSchema = z.object({
    username: z.string().optional(),
    email: z.string().optional(),
    sex: z.enum(['male','female',"all"]).default('all').transform((data)=>data.toString()),
    status: z.enum(['active','freeze','banned',"all"]).default('all').transform((data)=>data.toString()),
    createdAt: z.object({
        from: z.date({message: "创建开始时间"}).or(z.undefined()),
        to: z.union([z.date({message: "创建结束时间"}), z.undefined()]).optional(),
    }),
})

export const EditUserSchema = UserSchema.omit({
    userId: true,
    createdAt: true
}).merge(z.object({userId: z.coerce.number({message: "用户id不能为空"})}))

export const UserOmitCreatedAtPartial = UserSchema.omit({createdAt: true}).partial();

export const CreateUserSchema = UserSchema.omit({userId: true, sex: true, status: true}).merge(z.object({
    sex: z.enum(['male', 'female'],{message:"性别不能为空"}).transform((data) => data.toString()),
}))

export const RoleSearchFormSchema=z.object({
    roleCode:z.string().optional(),
    roleName:z.string().optional(),
})

export const CreateRoleFormSchema=z.object({
    roleCode:z.string().nonempty("角色编码不能为空"),
    roleName:z.string().nonempty("角色名不能为空"),
    detail:z.string().nonempty("详情说明不能为空"),
})
